package com.ruoyi.basedata.controller;

import com.ruoyi.basedata.domain.Supplies;
import com.ruoyi.basedata.domain.vo.SuppliesVO;
import com.ruoyi.basedata.service.ISuppliesService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 供应商Controller
 *
 * @author lvql
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/supplies")
public class SuppliesController extends BaseController {
    @Autowired
    private ISuppliesService suppliesService;

    //    @RequiresPermissions("basedata:supplies:list")
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        Supplies supplies = new Supplies();
        List<SuppliesVO> list = suppliesService.selectSuppliesList(supplies);
        return AjaxResult.success(list);
    }

    /**
     * 查询供应商列表
     */
    @RequiresPermissions("basedata:supplies:list")
    @GetMapping("/list")
    public TableDataInfo list(Supplies supplies) {
        startPage();
        List<SuppliesVO> list = suppliesService.selectSuppliesList(supplies);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    @RequiresPermissions("basedata:supplies:export")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Supplies supplies) {
        List<SuppliesVO> list = suppliesService.selectSuppliesList(supplies);
        ExcelUtil<SuppliesVO> util = new ExcelUtil<SuppliesVO>(SuppliesVO.class);
        util.exportExcel(response, list, "供应商数据");
    }

    /**
     * 获取供应商详细信息
     */
    @RequiresPermissions("basedata:supplies:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(suppliesService.selectSuppliesById(id));
    }
    /**
     * 获取供应商数量
     */
    @RequiresPermissions("basedata:supplies:query")
    @GetMapping(value = "getSupCount")
    public AjaxResult getSupCount() {
        return success(suppliesService.getSupCount());
    }
    /**
     * 新增供应商
     */
    @RequiresPermissions("basedata:supplies:add")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SuppliesVO supplies) {
        return toAjax(suppliesService.insertSupplies(supplies));
    }

    /**
     * 修改供应商
     */
    @RequiresPermissions("basedata:supplies:edit")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SuppliesVO supplies) {
        return toAjax(suppliesService.updateSupplies(supplies));
    }

    /**
     * 删除供应商
     */
    @RequiresPermissions("basedata:supplies:remove")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(suppliesService.deleteSuppliesByIds(ids));
    }

}
